const Utils = require("../../utils/index.js")

function my_install(router, connection) {

    // 获取goods商品
    router.post('/get_goods', async (ctx, next) => {

        let pageIndex = ctx.request.body.pageIndex
        let pageSize = ctx.request.body.pageSize
        let goodsName = ctx.request.body.goodsName || ''

        // 查询商品总数
        const totalPromise = await new Promise((resolve , reject)=>{
            let sql_str = `SELECT count(*) as total FROM tgoods where title like '%${goodsName}%';`
            connection.query(sql_str, function (error, results, fields) {
                if (error) {
                    throw error
                } else {
                    resolve(results)
                }
            })
        })
        ctx.body = await new Promise((resolve, reject) => {
            
            let sql_str = `SELECT * FROM tgoods where title like '%${goodsName}%' LIMIT ${pageSize} OFFSET ${(pageIndex - 1) * pageSize};`
            connection.query(sql_str, function (error, results, fields) {
                if (error) {
                    throw error
                } else {
                    resolve({
                        value: results,
                        total: totalPromise[0].total,
                        pageIndex,
                        pageSize,
                        code: 200
                    })
                }
            })
        })
    })


    // 根据商品类别 获取goods商品
    router.post('/get_goods_by_categoryItemId', async (ctx, next) => {
        ctx.body = await new Promise((resolve, reject) => {
            let pageIndex = ctx.request.body.pageIndex
            let pageSize = ctx.request.body.pageSize
            let categoryItemId = ctx.request.body.categoryItemId
            let sql_str = `SELECT * FROM tgoods where categoryItemId='${categoryItemId}' LIMIT ${pageSize} OFFSET ${(pageIndex) * pageSize} ;`


            connection.query(sql_str, function (error, results, fields) {
                if (error) {
                    throw error
                } else {
                    resolve({
                        value: results
                    })
                }
            })
        })
    })


    // 根据id 获取goods商品的详情
    router.post('/get_goods_by_id', async (ctx, next) => {
        ctx.body = await new Promise((resolve, reject) => {
            let id = ctx.request.body.id
            let sql_str = `
                SELECT t1.*, 
                t2.id as lunbo_id, t2.img as lunbo_img , 
                t3.id as detail_id, t3.img as detail_img
                from tgoods t1 
                LEFT JOIN
                goodsBanner t2 ON t1.id = t2.goodsId
                LEFT JOIN
                goodsDetail t3 ON t1.id = t3.goodsId
                WHERE
                t1.id='${id}';
            `


            connection.query(sql_str, function (error, results, fields) {
                if (error) {
                    throw error
                } else {
                    let tmp_goods = {}
                    for (let i = 0; i < results.length; i++) {
                        let item = JSON.parse(JSON.stringify(results[i]))
                        if (!tmp_goods.id) {
                            tmp_goods = item
                            delete tmp_goods.lunbo_img
                            delete tmp_goods.lunbo_id
                            delete tmp_goods.detail_id
                            delete tmp_goods.detail_img
                            tmp_goods.lunbo_arr = []
                            tmp_goods.detail_arr = []
                        }

                        if (item.lunbo_id && item.lunbo_img) {
                            if (Utils.array_find_item(tmp_goods.lunbo_arr , item , 'lunbo_id') == -1) {
                                tmp_goods.lunbo_arr.push({
                                    lunbo_img: item.lunbo_img,
                                    lunbo_id: item.lunbo_id
                                })
                            }
                        }

                        if (item.detail_id && item.detail_img) {
                            if (Utils.array_find_item(tmp_goods.detail_arr , item , 'detail_id') == -1) {
                                tmp_goods.detail_arr.push({
                                    detail_img: item.detail_img,
                                    detail_id: item.detail_id
                                })
                            }
                        }
                        

                    }

                    resolve({
                        // value: results
                        value: tmp_goods
                    })
                }
            })
        })
    })


    // 根据id 删除goods商品
    router.post('/delete_goods_by_id', async (ctx, next) => {
        ctx.body = await new Promise((resolve, reject) => {
            let id = ctx.request.body.id
            let sql_str = `
                Delete 
                from tgoods
                WHERE
                tgoods.id='${id}';
            `

            connection.query(sql_str, function (error, results, fields) {
                if (error) {
                    throw error
                } else {
                    let res = JSON.parse(JSON.stringify(results))
                    if (res.affectedRows >= 1) {
                        resolve({
                            code: 200,
                            value: res,
                            msg: '删除成功'
                        })
                    } else {
                        resolve({
                            code: -1,
                            value: {},
                            msg: '删除失败'
                        })
                    }
                    
                }
            })
        })
    })
}
module.exports = { my_install }